<?php
/**
 * Yii view _form_service
 *
 * @author nguyenmanhluu
 * @since Jul 26, 2013 - 4:43:42 PM
 */
?>
<?php $typeText = $model->type == Service::TYPE_ROOM ? 'Class' : 'Service' ?>
<div class="box span12">
    <div class="box-header">
        <h2>
            <i class="icon-plus"></i>
            <?php echo $model->isNewRecord ? 'Add' : 'Edit' ?>
            <?php echo $typeText ?>
        </h2>
        <div class="box-icon">
            <?php $action   = Service::TYPE_ROOM ? 'room' : 'index' ?>
            <?php if (request()->getQuery('at', '') != ''): ?>

            <?php else: ?>
                <a href="<?php echo $this->createUrl("service/$action"); ?>">
                    <i class="icon-arrow-left"></i>
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="box-content">
        <?php
        $form_id = strtolower($typeText) . '-form';
        $form_id .= $model->isNewRecord ? '-add' : '-edit';

        $form = $this->beginWidget('CActiveForm', array(
            'id'                     => $form_id,
//            'enableAjaxValidation' => true,
            'enableClientValidation' => true,
            'htmlOptions'            => array(
                'class'   => 'form-horizontal',
                'enctype' => 'multipart/form-data',
            ),
            'focus'                  => array($model, 's_name'),
            'clientOptions'          => array(
                'validateOnSubmit' => true,
            ),
        ));
        ?>
        <div class="row-fluid">
            <div class="span6">
                <?php if (Yii::app()->user->hasFlash('form_success')): ?>
                    <div class="pull-left">
                        <div class="alert alert-success">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            <strong>All done!</strong>
                            <?php echo Yii::app()->user->getFlash('form_success'); ?>
                        </div>
                    </div>
                <?php elseif (Yii::app()->user->hasFlash('form_error')): ?>
                    <div class="pull-left">
                        <div class="alert alert-error">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            <strong>Oh snap!</strong> 
                            <?php echo Yii::app()->user->getFlash('form_error'); ?>
                        </div>
                    </div>
                <?php endif; ?>
                <?php
                if ($this->isBussiness):
                    $ukey               = Users::getUkeyById(Yii::app()->user->id);
                    $biz                = Users::getRecordByUkey($ukey);
                    $model->address     = $biz->address;
                    $model->address2    = $biz->address2;
                    $model->postal_code = $biz->postal_code;
                    $model->latitude    = $biz->latitude;
                    $model->longitude   = $biz->longitude;
                endif;
                $model->author_id = ($model->isNewRecord && !empty(user()->getState('bizId'))) ? user()->getState('bizId') : $model->author_id;
                ?>
                <?php echo CHtml::errorSummary($model); ?>
                <fieldset>            

                    <?php if (Yii::app()->controller->isAdmin): ?>
                        <div class="control-group">
                            <?php echo $form->label($model, 'author_id', array('class' => 'control-label')) ?>
                            <div class ="controls">                                                        
                                <?php
                                echo $form->dropDownList($model, 'author_id', Users::getAllBussiness(), array(
                                    'data-rel'         => "chosen",
                                    'data-placeholder' => "Type Merchant name...",
                                ));
                                ?>
                                <font color="red"><?php echo $form->error($model, 'author_id'); ?></font>                   
                            </div>
                        </div>            
                    <?php endif; ?>

                    <div class="control-group">
                        <?php echo $form->label($model, 'category_id', array('class' => 'control-label')) ?>
                        <div class ="controls" id="Select_Cate">
                            <?php if (!$model->isNewRecord): ?>
                                <?php $biz_id = $this->isAdmin ? $model->author_id : user()->id ?>
                                <?php $data   = CHtml::listData(Category::GetAllCategory($biz_id), 'id', 'name_cat', 'group'); ?>
                                <?php echo $form->dropDownList($model, 'category_id', $data); ?>
                            <?php endif; ?>
                            <font color="red"><?php echo $form->error($model, 'category_id'); ?></font>
                        </div>
                    </div>
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 's_name', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <?php
                            echo $form->textField($model, 's_name', array(
                                'placeholder' => $model->getAttributeLabel('s_name'),
                                'class'       => 'input-file uniform_on input-reset'
                            ));
                            ?>
                            <font color="red"><?php echo $form->error($model, 's_name'); ?></font>
                        </div>
                    </div>
                    <div class="control-group">
                        <?php echo $form->label($model, 'duration', array('class' => 'control-label')) ?>
                        <div class ="controls">                
                            <?php echo $form->dropDownList($model, 'duration', Service::getDurationArray()); ?>
                            <font color="red"> <?php echo $form->error($model, 'duration'); ?> </font>
                        </div>
                        <font color="red"> <?php echo $form->error($model, 'duration'); ?> </font>
                    </div>
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 'price', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <div class="input-prepend">
                                <span class="add-on">$</span>
                                <?php
                                echo $form->textField($model, 'price', array(
                                    'placeholder' => $model->getAttributeLabel('price'),
                                    'class'       => 'input-file uniform_on input-reset span6'
                                ));
                                ?>
                            </div>
                            <font color="red"><?php echo $form->error($model, 'price'); ?></font>
                        </div>
                    </div>    
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 'address', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <?php
                            echo $form->textField($model, 'address', array(
                                'placeholder' => $model->getAttributeLabel('address'),
                                'class'       => 'input-file uniform_on input-reset'
                            ));
                            ?>
                            <font color="red"><?php echo $form->error($model, 'address'); ?></font>
                        </div>
                    </div>
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 'address2', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <?php
                            echo $form->textField($model, 'address2', array(
                                'placeholder' => 'Optional',
                                'class'       => 'input-file uniform_on input-reset'
                            ));
                            ?>
                            <font color="red"><?php echo $form->error($model, 'address2'); ?></font>
                        </div>
                    </div>
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 'postal_code', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <?php
                            echo $form->textField($model, 'postal_code', array(
                                'placeholder' => $model->getAttributeLabel('postal_code'),
                                'class'       => 'input-file uniform_on input-reset'
                            ));
                            ?>
                            <font color="red"><?php echo $form->error($model, 'postal_code'); ?></font>
                        </div>
                    </div>
                    <!--                    <div class="control-group">
                    <?php echo $form->labelEx($model, 'latitude', array('class' => 'control-label')); ?>
                                            <div class ="controls">
                    <?php
                    echo $form->textField($model, 'latitude', array(
                        'placeholder' => $model->getAttributeLabel('latitude'),
                        'class'       => 'input-file uniform_on input-reset'
                    ));
                    ?>
                                                <font color="red"><?php echo $form->error($model, 'latitude'); ?></font>
                                            </div>
                                        </div>    
                                        <div class="control-group">
                    <?php echo $form->labelEx($model, 'longitude', array('class' => 'control-label')); ?>
                                            <div class ="controls">
                    <?php
                    echo $form->textField($model, 'longitude', array(
                        'placeholder' => $model->getAttributeLabel('longitude'),
                        'class'       => 'input-file uniform_on input-reset'
                    ));
                    ?>
                                                <font color="red"><?php echo $form->error($model, 'longitude'); ?></font>
                                            </div>
                                        </div>    -->
                    <div class="control-group">
                        <?php echo $form->label($model, 'recover', array('class' => 'control-label')) ?>
                        <div class ="controls">                
                            <?php
                            echo $form->dropDownList($model, 'recover', Service::getRecoverArray());
                            ?>
                            <font color="red"> <?php echo $form->error($model, 'duration'); ?> </font>
                        </div>
                        <font color="red"> <?php echo $form->error($model, 'duration'); ?> </font>
                    </div>
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 'description', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <?php
                            echo $form->textArea($model, 'description', array(
                                'class'       => 'cleditor',
                                'placeholder' => $model->getAttributeLabel('description'),))
                            ?>
                            <font color="red"><?php echo $form->error($model, 'description'); ?></font>
                        </div>
                    </div>
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 'images', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <div class="user_photo">
                                <?php if ($model->sImages): ?>
                                    <?php foreach ($model->sImages as $img): ?>
                                        <div class="row-fluid box">
                                            <div class="span6">                                        
                                                <img src="<?php echo $img->publicImage ?>" alt="User photo">
                                            </div>
                                            <!--<div class="clear"></div>-->                                    
                                        </div>
                                        <div class="span6">

                                            <label class="radio" style="padding-left: 0!important">
                                                <?php
                                                echo $form->radioButton($model, 'images', array(
                                                    'value'        => $img->id,
                                                    'uncheckValue' => null,
                                                    'id'           => "Service_images" . $img->id
                                                ));
                                                ?>
                                                <label for="Service_images<?php echo $img->id ?>">Set default</label>
                                            </label>

                                            <p>
                                                <input type="checkbox" name="delServiceImage[]" 
                                                       value="<?php echo $img->id ?>" id="delImg<?php echo $img->id ?>"/>
                                                <label for="delImg<?php echo $img->id ?>">Delete</label>
                                            </p>
                                        </div>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <img src="<?php echo $model->publicImage ?>" alt="User photo">
                                <?php endif; ?>
                            </div>                                
                            <input type="file" name="file[]" multiple />
                            <font color="red"><?php echo $form->error($model, 'images'); ?></font>
                        </div>
                    </div>
                    <div class="control-group">
                        <?php echo $form->labelEx($model, 'avatar', array('class' => 'control-label')); ?>
                        <div class ="controls">
                            <div class="user_photo">
                                <?php if ($model): ?>
                                    <div class="row-fluid box">
                                        <div class="span6">                                        
                                            <img src="<?php echo $model->publicAvatar ?>" alt="User photo">
                                        </div>
                                        <div class="span6">
                                            <p>
                                                <input type="checkbox" name="delServiceAvarta[]" 
                                                       value="<?php echo $model->sid ?>" id="delAva<?php echo $model->sid ?>"/>
                                                <label for="delAva<?php echo $model->sid ?>">Delete</label>
                                            </p>
                                        </div>
                                        <div class="clear"></div>                                    
                                    </div>
                                <?php else: ?>
                                    <img src="<?php echo $model->publicAvatar ?>" alt="User photo">
                                <?php endif; ?>
                            </div>                                
                            <input type="file" name="fileAvatar" />
                            <font color="red"><?php echo $form->error($model, 'avatar'); ?></font>
                            <p class="muted">Recommended resolution: 50px X 50px</p>
                        </div>
                    </div>

                    <div class="control-group">
                        <?php // echo $form->labelEx($model, 'book_online', array('class' => 'control-label')); ?>
                        <label class="control-label"></label>
                        <div class ="controls">
                            <?php
                            echo $form->checkBox($model, 'book_online', array(
                                'class' => 'input-file uniform_on input-reset'
                            ))
                            ?>
                            <label for="Service_book_online">Booking online</label>
                            <font color="red"><?php echo $form->error($model, 'book_online'); ?></font>
                        </div>
                        <div class="controls">
                            <?php
                            echo $form->checkBox($model, 'travel', array(
                                'class' => 'input-file uniform_on input-reset'
                            ))
                            ?>
                            <label for="Service_travel">Business services that travel to me</label>
                        </div>

                        <div class="control-group">
                            <?php echo $form->labelEx($model, 'weight', array('class' => 'control-label')); ?>
                            <div class ="controls">
                                <?php
                                echo $form->textField($model, 'weight', array(
                                    'placeholder' => 'Enter weight (service priority)',
                                    'class'       => 'input-file uniform_on input-reset'
                                ));
                                ?>
                                <font color="red"><?php echo $form->error($model, 'weight'); ?></font>
                            </div>
                        </div>
                    </div>
                </fieldset>
            </div>
            <div class="span6">
                <?php if (Yii::app()->user->hasFlash('error')): ?>
                    <div class="alert alert-error">
                        <?php echo Yii::app()->user->getFlash('error') ?>
                    </div>
                <?php endif; ?>
                <div class="box-content">
                    <fieldset>
                        <div class="control-group">

                            <div id="table_usr">
                                <label for="prependedInput">Assign staff:</label>
                                <?php if (Yii::app()->controller->isBussiness): ?>
                                    <table class="table table-bordered table-striped table-condensed">
                                        <thead>
                                            <tr>
                                                <th>Name</th>
                                                <th>Email</th>
                                                <th>Assign</th>
                                            </tr>
                                        </thead>   
                                        <tbody>

                                        </tbody>
                                    </table>  
                                <?php endif; ?>
                                <?php if (Yii::app()->controller->isAdmin): ?>
                                    <table class="table table-bordered table-striped table-condensed">
                                        <thead>
                                            <tr>
                                                <th>Name</th>
                                                <th>Email</th>
                                                <th>Assign</th>
                                            </tr>
                                        </thead>   
                                        <tbody>

                                        </tbody>
                                    </table>  
                                <?php endif; ?>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>
        </div>
        <?php echo $this->renderPartial('extra/index', array('model' => $model)) ?>
        <div class="clearfix"></div>
        <div class="form-actions">
            <!--<div class ="controls">-->
            <button type="submit" class="btn btn-primary" name="submit" value="submit"><i class="icon-ok"></i>
                &nbsp;Save</button>&nbsp;
            <button class="btn btn-primary" type="submit" name="submit" value="continue"><i class="icon-ok"></i>
                &nbsp; Save and continue edit</button>&nbsp;
            <a target="_blank" class="btn btn-primary" href="<?php echo Yii::app()->createUrl('service/create', array('t' => $model->type)) ?>">
                <i class="icon-plus"></i>
                &nbsp; Add More Service</a>&nbsp;
            <?php
            if (Yii::app()->request->getQuery('at', '') == 'service' && user()->isAdmin()):
                $bizKey = user()->getState('bizKey', '');
                ?>
                <a href="<?php echo Yii::app()->createUrl('bizuser/edit', array('pk' => $bizKey, "#" => "service")) ?>" class="btn">Cancel</a>&nbsp;
            <?php else: ?>
                <a href="<?php echo Yii::app()->createUrl('service/index', array('type' => $model->type)) ?>" class="btn">Cancel</a>&nbsp;
            <?php endif; ?>
            <!--</div>-->
        </div>
        <?php $this->endWidget(); ?>
    </div>
</div>
<script type="text/javascript">
    $('#Service_author_id').change(function() {
        $.ajax({
            url: '<?php echo Yii::app()->createUrl('Service/GetStaffByUkey') ?>',
            type: 'POST',
            data: {
                ukey: $(this).val(),
                type: '<?php echo $model->isNewRecord ? 'add' : 'edit' ?>',
                id: '<?php echo $model->isNewRecord ? 'noid' : $model->sid ?>'
            }
        }).done(function(msg) {
            $('#table_usr').html(msg);
        });
        //fill biz info
        $.post('<?php echo Yii::app()->createUrl('Service/GetBizInfo') ?>', {
            uid: $(this).val(),
        }, function(data) {
            if (typeof (data) == 'object') {
                $('#Service_address').val(data.address);
                $('#Service_address2').val(data.address2);
                $('#Service_postal_code').val(data.postal_code);
//                $('#Service_latitude').val(data.ltt);
//                $('#Service_longitude').val(data.lng);
            }
        }, 'json');
        getCategoryBelongBiz();
    });

    var uid;
    function getCategoryBelongBiz()
    {
<?php if ($this->isAdmin): ?>
            uid = $('#Service_author_id').val();
<?php endif; ?>
<?php if ($this->isBussiness): ?>
            uid = '<?php echo Yii::app()->user->id ?>';
<?php endif; ?>
        $.post('<?php echo Yii::app()->createUrl('Service/GetCategoryBelongBiz') ?>', {
            uid: uid,
            is_new: <?php echo $model->isNewRecord ? 0 : $model->sid ?>
        }, function(msg) {
            $('#Select_Cate').html('').append(msg);
        });
    }
    $(document).ready(function() {
<?php if ($this->isAdmin): ?>
            uid = $('#Service_author_id').val();
<?php endif; ?>
<?php if ($this->isBussiness): ?>
            uid = '<?php echo Yii::app()->user->id ?>';
<?php endif; ?>
        $.ajax({
            url: '<?php echo Yii::app()->createUrl('Service/GetStaffByUkey') ?>',
            type: 'POST',
            data: {
                ukey: uid,
                type: '<?php echo $model->isNewRecord ? 'add' : 'edit' ?>',
                id: '<?php echo $model->isNewRecord ? 'noid' : $model->sid ?>'
            }
        }).done(function(msg) {
            $('#table_usr').html(msg);
        });
    });
<?php if ($model->isNewRecord): ?>
        getCategoryBelongBiz();
<?php endif; ?>
</script>
